define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'editable'], function ($, undefined, Backend, Table, Form, undefined) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'dahui/newtk/index',
                    add_url: 'dahui/newtk/add',
                    edit_url: 'dahui/newtk/edit',
                    del_url: 'dahui/newtk/del',
                    multi_url: 'dahui/newtk/multi',
                    import_url: 'dahui/newtk/import',
                    table: 'dahui_newtk',
                }
            });

            var table = $("#table");
            var strfilter = "";
            var strop = "";
            //在普通搜索提交搜索前
            table.on('common-search.bs.table', function (event, table, query) {

                var filter = {};//创建空对象
                //拷贝不为空的字段
                for (var key in query.filter) {
                    if (query.filter[key]) {
                        filter[key] = query.filter[key];
                    }
                }
                console.log(filter);

                strfilter = JSON.stringify(filter);
                strop = JSON.stringify(query.op);
                //console.log(query);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                showToggle: false,
                search: false,
                sortName: 'id',
                showExport: false,
                searchFormVisible: true,
                exportTypes: ['csv'],
                dblClickToEdit: false, //是否启用双击编辑
                columns: [
                    [
                        {checkbox: true},
                        //{field: 'id', title: __('Id'), operate:false},
                        {field: 'taobaoname', title: __('Taobaoname'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'name', title: __('Name'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'idcardstate', title: __('Idcardstate'), searchList: {"正确":__('正确'),"错误":__('错误'), "重复":__('重复')}, formatter: Table.api.formatter.normal},
                        {
                            field: 'idnumber',
                            title: __('身份证号码'),
                            editable: {
                                emptytext: '无',
                                rows:10,
                                tpl: "<textarea rows='10' cols='50'></textarea>",
                                success: function (response, newValue) {
                                    //$(".btn-refresh").trigger("click");
                                }
                            }
                        },
                        {field: 'jndate', title: __('退款月份')},
                        {field: 'monthspaid', title: __('退款月数'), operate:false},
                        {field: 'paymentbase', title: __('Paymentbase')},
                        {field: 'paymentamount', title: __('应缴纳金额'), operate:false},
                        {field: 'newje', title: __('实际缴纳金额'), operate:false},

                        {field: 'zfbname', title: __('支付宝账号'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'tkname', title: __('支付宝姓名'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'ytje', title: __('退款金额'), operate:false},
                        //{field: 'newje', title: __('Newje'), operate:false},
                        {field: 'djman', title: __('制表'), operate:false},
                        {field: 'chman', title: __('初核')},
                        {field: 'fhman', title: __('复核')},
                        //{field: 'payingcompany', title: __('Payingcompany')},
                        {field: 'tkxzt', title: __('Tkxzt'), searchList: {"待初审":__('待初审'),"待终审":__('待终审'),"已终审":__('已终审'),"":__('')}, formatter: Controller.api.formatter.tkxzt},
                        {field: 'refundornot', title: __('退款状态'), searchList: {"未退款":__('未退款'),"已退款":__('已退款'),"退款失败":__('退款失败'),"":__('')}, formatter: Controller.api.formatter.tkstatus},
                        {
                            field: 'remark',
                            title: __('备注'),
                            editable: {
                                emptytext: '无',
                                rows:10,
                                tpl: "<textarea rows='10' cols='50'></textarea>",
                                success: function (response, newValue) {
                                    //$(".btn-refresh").trigger("click");
                                }
                            },
                            operate: 'LIKE %...%',
                            placeholder: '模糊搜索，*表示任意字符'
                        },
                        {field: 'tkdate', title: __('Tkdate'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},

                        //{field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            //点击退款状态
            $(document).on("dblclick", ".tkstatus[data-id]", function () {

                if (Config.audit) {
                    var url = 'dahui/newtk/changetstatus';
                    var id = $(this).data('id');
                    var _this = $(this);
                    Fast.api.ajax({
                        url: url,
                        data: {ids: id},
                    }, function (data, ret) {
                        console.log(ret.data.refundornot);
                        _this.html(ret.data.refundornot);
                        if (ret.data.refundornot != '已退款') {
                            _this.addClass('text-danger');
                        } else {
                            _this.removeClass('text-danger')
                        }
                        //$(".btn-refresh").trigger("click");
                        //return false;
                    });
                } else if (Config.tuikuan) {
                    var url = 'dahui/newtk/changetstatus4';
                    var id = $(this).data('id');
                    var _this = $(this);
                    Fast.api.ajax({
                        url: url,
                        data: {ids: id},
                    }, function (data, ret) {
                        console.log(ret.data.refundornot);
                        _this.html(ret.data.refundornot);
                        if (ret.data.refundornot != '已退款') {
                            _this.addClass('text-danger');
                        } else {
                            _this.removeClass('text-danger')
                        }
                        //$(".btn-refresh").trigger("click");
                        //return false;
                    });
                }
                return false;
            });

            //初核+复核
            $(document).on("dblclick", ".tkxzt[data-id]", function () {

                var url = 'dahui/newtk/chuhefuhe';
                if (Config.fuhe && Config.chuhe) {


                } else if (Config.fuhe) {
                    url = 'dahui/newtk/fuhe';
                } else if (Config.chuhe) {
                    url = 'dahui/newtk/chuhe';
                } else {
                    return false;
                }
                var id = $(this).data('id');
                var _this = $(this);
                Fast.api.ajax({
                    url: url,
                    data: {ids: id},
                }, function (data, ret) {
                    console.log(ret.data.tkxzt);
                    _this.html(ret.data.tkxzt);
                    if (ret.data.tkxzt != '已终审') {
                        _this.addClass('text-danger');
                    } else {
                        _this.removeClass('text-danger')
                    }
                    $(".btn-refresh").trigger("click");
                });
                return false;
            });

            // 批量初核
            $(document).on("click", ".btn-plchuhe", function () {
                if (Config.chuhe) {
                    var ids = Table.api.selectedids(table);
                    var url = 'dahui/newtk/plchuhe?ids=' + ids;
                    var str = "确定批量初核?";
                    var that = this;
                    Layer.confirm(str, {icon: 3, title: '提示'}, function (index) {
                        Backend.api.ajax({
                            url: url,
                        }, function () {
                            Layer.close(index);
                            $(".btn-refresh").trigger("click");
                        });
                    });
                }
            });
            // 批量复核
            $(document).on("click", ".btn-plfuhe", function () {
                if (Config.chuhe) {
                    var ids = Table.api.selectedids(table);
                    var url = 'dahui/newtk/plfuhe?ids=' + ids;
                    var str = "确定批量复核?";
                    var that = this;
                    Layer.confirm(str, {icon: 3, title: '提示'}, function (index) {
                        Backend.api.ajax({
                            url: url,
                        }, function () {
                            Layer.close(index);
                            $(".btn-refresh").trigger("click");
                        });
                    });
                }
            });

            // 批量退款
            $(document).on("click", ".btn-pltk", function () {
                if (Config.audit) {
                    var ids = Table.api.selectedids(table);
                    var url = 'dahui/newtk/pltk?ids=' + ids;

                    var that = this;
                    Layer.confirm(__('确定批量退款?'), {icon: 3, title: '提示'}, function (index) {
                        Backend.api.ajax({
                            url: url,
                        }, function () {
                            Layer.close(index);
                            $(".btn-refresh").trigger("click");
                        });
                    });
                }
            });

            // 检查身份证
            $(document).on("click", ".btn-jcsfz", function () {
                if (Config.audit) {
                    var url = 'dahui/newtk/jcsfz';

                    var that = this;
                    Layer.confirm(__('确定检查身份证?'), {icon: 3, title: '提示'}, function (index) {
                        Backend.api.ajax({
                            url: url,
                        }, function () {
                            Layer.close(index);
                            $(".btn-refresh").trigger("click");
                        });
                    });
                }
            });



            //导出
            $(document).on("click", ".btn-myexcel-export", function () {
                top.location.href = "newtk/import0?filter=" + strfilter + "&op=" + strop;
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                tkstatus: function (value, row, index) {
                    if (row.refundornot != '已退款') {
                        return [
                            '<a href="javascript:;" class="tkstatus text-danger" data-id= "' + row.id + '" > ' + value + '</a>'
                        ].join("")

                    } else {
                        return [
                            '<a href="javascript:;" class="tkstatus" data-id= "' + row.id + '" > 已退款 </a>'
                        ].join("")
                    }
                },
                tkxzt: function (value, row, index) {
                    if (row.tkxzt != '已终审') {
                        return [
                            '<a href="javascript:;" class="tkxzt text-danger" data-id= "' + row.id + '" > ' + value + '</a>'
                        ].join("")

                    } else {
                        return [
                            '<a href="javascript:;" class="tkxzt" data-id= "' + row.id + '" > 已终审 </a>'
                        ].join("")
                    }
                }
            }
        }
    };
    return Controller;
});